// operation-alert-model.js - A mongoose model
// 
// See http://mongoosejs.com/docs/models.html
// for more of what you can do here.
module.exports = function (app) {
  const mongooseClient = app.get('mongooseClient');
  const { Schema } = mongooseClient;
  const operationAlert = new Schema({
    name: { type: String },
    title: { type: String },
    description: { type: String },
    contents: [ { type: Schema.Types.Mixed } ],
    alert_for_time: { type: Schema.Types.Date },
    to: {
      operations: [
        { 
          oid: { type: Schema.Types.ObjectId },
          filters: [{ type: Schema.Types.Mixed }], //filter to operators of operation
          data: { type: Schema.Types.Mixed }
        }
      ], // send all operation operators
      operators: [
        { 
          oid: { type: Schema.Types.ObjectId },
          data: { type: Schema.Types.Mixed }
        }
      ]
    },
    from: {
      operator: {
        oid: { type: Schema.Types.ObjectId },
        data: { type: Schema.Types.Mixed }
      }
    },
    reminders: [
      {
        start: {
          time: { type: Schema.Types.Date },
          expression: { type: String } //like alert_for_time - 5d(before alert for time 5 days )
        },
        end: { 
          time: { type: Schema.Types.Date },
          expression: { type: String } //like alert_for_time - 5d(before alert for time 5 days )
        },
        interval:{
          period: { type: String }, //year(y), month(m), week(w), day(d), hour(h), minute(m), second(s), milisecond(ms),
          number: { type: Number }, //times to divid from start to end
        },
        repeat: {
          period: { type: String }, //year(y), month(m), week(w), day(d), hour(h), minute(m), second(s), milisecond(ms),
          number: { type: Number }, //times to repeat
        },
        data: { type: Schema.Types.Mixed }, //use to override info from alert
        levels: {
          type: Schema.Types.Mixed
        } //can override alert levels
      }
    ],
    levels: { //level will be populated by client base on levels
      urgent: { type: String },//for example: now - alert_for_time < 1d(1h,1m)
      warn: { type: String },
      info: { type: String }
    },//urgent, warn, info or other
    data: { type: Schema.Types.Mixed }
  }, {
    timestamps: true
  });

  return mongooseClient.model('operationAlert', operationAlert);
};
